package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.product.model.BaseCategoryTrademark;
import com.atguigu.gmall.product.mapper.BaseCategoryTrademarkMapper;
import com.atguigu.gmall.product.model.CategoryTrademarkVo;
import com.atguigu.gmall.product.service.BaseCategoryTrademarkService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 分类品牌中间表 业务实现类
 *
 * @author atguigu
 * @since 2023-02-22
 */
@Service
public class BaseCategoryTrademarkServiceImpl extends ServiceImpl<BaseCategoryTrademarkMapper, BaseCategoryTrademark> implements BaseCategoryTrademarkService {

    @Override
    public void saveCategoryTrademark(CategoryTrademarkVo vo) {
        //1.判断对象是否有值
        if (vo != null) {
            //2.循环构建品牌分类关系对象
            List<BaseCategoryTrademark> categoryTrademarkList = vo.getTrademarkIdList().stream().map(tradeMarkId -> {
                BaseCategoryTrademark categoryTrademark = new BaseCategoryTrademark();
                categoryTrademark.setCategory3Id(vo.getCategory3Id());
                categoryTrademark.setTrademarkId(tradeMarkId);
                return categoryTrademark;
            }).collect(Collectors.toList());

            //3.批量新增
            this.saveBatch(categoryTrademarkList);
        }
    }

    /**
     * 解除品牌分类关联
     *
     * @param category3Id
     * @param trademarkId
     * @return
     */
    @Override
    public void deleteCategoryTrademark(Long category3Id, Long trademarkId) {
        //方式一:选择逻辑删除
        //LambdaQueryWrapper<BaseCategoryTrademark> queryWrapper = new LambdaQueryWrapper<>();
        //queryWrapper.eq(BaseCategoryTrademark::getCategory3Id, category3Id);
        //queryWrapper.eq(BaseCategoryTrademark::getTrademarkId, trademarkId);
        //this.remove(queryWrapper);

        //方式二:选择物理删除
        this.getBaseMapper().deleteCategoryTrademark(category3Id, trademarkId);
    }
}
